package com.flower.domain.service.impl;

import com.flower.auth.common.enums.IsDeletedFlagEnum;
import com.flower.auth.infra.basic.entity.AuthPermission;
import com.flower.auth.infra.basic.entity.AuthRolePermission;
import com.flower.auth.infra.basic.service.AuthPermissionService;
import com.flower.auth.infra.basic.service.AuthRolePermissionService;
import com.flower.auth.infra.basic.service.AuthRoleService;
import com.flower.domain.convert.AuthPermissionBOConverter;
import com.flower.domain.convert.AuthRolePermissionBOConverter;
import com.flower.domain.entity.AuthPermissionBO;
import com.flower.domain.entity.AuthRolePermissionBO;
import com.flower.domain.service.AuthPermissionDomainService;
import com.flower.domain.service.AuthRolePermissionDomainService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * @author lavender
 * @version 1.0
 * @date 2024-07-16 21:24
 */
@Service
public class AuthRolePermissionDomainServiceImpl implements AuthRolePermissionDomainService {

    @Resource
    private AuthRolePermissionService  service;




    @Override
    public Boolean add(AuthRolePermissionBO authPermissionBO) {

        List<AuthRolePermission> list = new LinkedList<>();

        Long roleId = authPermissionBO.getRoleId();
        authPermissionBO.getPermissionIds().forEach(permissionId -> {
            AuthRolePermission authRolePermission = new AuthRolePermission();
            authRolePermission.setRoleId(roleId);
            authRolePermission.setPermissionId(permissionId);
            authRolePermission.setIsDelete(IsDeletedFlagEnum.UN_DELETED.getCode());
           list.add(authRolePermission);
        });

        service.insertBatch(list);



        return true;

    }

    @Override
    public Boolean update(AuthRolePermissionBO authPermissionBO) {
        AuthRolePermission authRolePermission = AuthRolePermissionBOConverter.INSTANCE.convertDTOToBO(authPermissionBO);
        Integer result = service.update(authRolePermission);
        return result > 0;
    }

    @Override
    public Boolean delete(AuthRolePermissionBO authPermissionBO) {
        AuthRolePermission authRolePermission = AuthRolePermissionBOConverter.INSTANCE.convertDTOToBO(authPermissionBO);
        authRolePermission.setIsDelete(IsDeletedFlagEnum.UN_DELETED.getCode());
        Integer result = service.update(authRolePermission);

        return result > 0;
    }
}
